<?php

class AtendimentoDao extends Dao {

    /**
     * @var SetorService
     */
    private $setorService;
    /**
     * @var MensagemService
     */
    private $mensagemService;
    /**
     * @var ClienteService
     */
    private $clienteService;

    public function injectDependencies() {
        $this->setorService = SetorService::getInstance();
        $this->mensagemService = MensagemService::getInstance();
        $this->clienteService = ClienteService::getInstance();
    }

    public function savePhoneCalled($ArDados) {
        $this->initTransaction();
        $this->tMysqli->escape($ArDados);

        $Chamado = new ChamadoTelefonico($ArDados['PhoneCalled'], $this->tMysqli);
        $Chamado->loadDataCreatedCall();
        $StSqlCreateNewPhoneCalled = TQuery::getSqlInsertNewCalled($Chamado);
        $this->tMysqli->exec($StSqlCreateNewPhoneCalled);
        $IDChamado = $this->tMysqli->lastInsertId();

        $Chamado->setDefaultDataInAllMessages($IDChamado);
        $StSqlInsertNewMessage = TQuery::getSqlInsertNewMessage($Chamado->getFirstMessage());
        $this->tMysqli->exec($StSqlInsertNewMessage);

        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function updateCalledByFilter($ArFilter) {
        $this->initTransaction();
        $this->tMysqli->escape($ArFilter);
        $StSqlUpdateCalled = TQuery::getSqlUpdateCalledByFilter($ArFilter);
        $Message = $this->mensagemService->generateMessage($ArFilter);
        $StSqlReleaseHistory = TQuery::getSqlReleaseMessageInHistory($Message);
        $this->tMysqli->exec($StSqlUpdateCalled);
        $this->tMysqli->exec($StSqlReleaseHistory);
        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function insertNote($Mensagem) {
        $this->initConnection();
        $StSqlReleaseHistory = TQuery::getSqlReleaseMessageInHistory($Mensagem);
        $this->tMysqli->exec($StSqlReleaseHistory);
        $BoRetorno = $this->tMysqli->lastQueryReturn();
        $this->closeConnection();
        return $BoRetorno;
    }

    public function changeSector($ArDados, $Mensagem) {
        $this->initTransaction();
        $this->tMysqli->escape($ArDados);
        $StSqlUpdateCalled = TQuery::getSqlUpdateCalledByFilter($ArDados);
        $StSqlReleaseHistory = TQuery::getSqlReleaseMessageInHistory($Mensagem);
        $this->tMysqli->exec($StSqlUpdateCalled);
        $this->tMysqli->exec($StSqlReleaseHistory);
        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function insertAnswer($Mensagem) {
        $this->initTransaction();
        $StSqlReleaseHistory = TQuery::getSqlReleaseMessageInHistory($Mensagem);
        $StSqlUpdateCalled = TQuery::getSqlUpdateCalledByFilter(array('EnStatus' => 'CLIENTE', 'IDChamado' => $Mensagem->getIDChamado()));
        $this->tMysqli->exec($StSqlReleaseHistory);
        $this->tMysqli->exec($StSqlUpdateCalled);
        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function changeResponsible($ArDados, $Mensagem) {
        $this->initTransaction();
        $this->tMysqli->escape($ArDados);
        $StSqlUpdateCalled = TQuery::getSqlUpdateCalledByFilter(array('IDResponsavel' => $ArDados['IDNewUser'], 'IDChamado' => $ArDados['IDChamado']));
        $StSqlReleaseHistory = TQuery::getSqlReleaseMessageInHistory($Mensagem);
        $this->tMysqli->exec($StSqlUpdateCalled);
        $this->tMysqli->exec($StSqlReleaseHistory);
        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function findById($IDChamado) {
        $this->initConnection();
        $this->tMysqli->escape($IDChamado);
        $StSql = TQuery::getSqlFindCalledById($IDChamado);
        $tQuery = $this->tMysqli->exec($StSql);
        while ($ArDadosChamado = mysqli_fetch_assoc($tQuery)) {
            $Chamado = Chamado::calledFactory($ArDadosChamado, $this->tMysqli);
            $Chamado->fill();
        }
        $this->closeConnection();
        return isset($Chamado) ? $Chamado : false;
    }

    public function advancedSearch($ArParameters) {
        $this->initConnection();
        $this->tMysqli->escape($ArParameters);
        $StSql = TQuery::getSqlAdvancedSearchCalled($ArParameters);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArCalledData = mysqli_fetch_assoc($tQuery)) {
             $Chamado = Chamado::calledFactory($ArCalledData, $this->tMysqli);
             $Chamado->fill();
             $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findPhoneCalledsBySector() {
        $StSql = TQuery::getSqlFindPhoneCalledBySector(Dracosys::getLoggedUser()->getTDepartamento()->getSetores(true)->getIDSetor());
        $this->initConnection();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArPhoneCalled = mysqli_fetch_assoc($tQuery)) {
             $Chamado = Chamado::calledFactory($ArPhoneCalled, $this->tMysqli);
             $Chamado->fill();
             $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findAllPhoneCalled() {
        $StSql = TQuery::getSqlFindAllPhoneCalled();
        $this->initConnection();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArPhoneCalled = mysqli_fetch_assoc($tQuery)) {
             $Chamado = Chamado::calledFactory($ArPhoneCalled, $this->tMysqli);
             $Chamado->fill();
             $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findLast20PhoneCalled() {
        $StSql = TQuery::getSqlFindLast20PhoneCalled();
        $this->initConnection();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArPhoneCalled = mysqli_fetch_assoc($tQuery)) {
             $Chamado = Chamado::calledFactory($ArPhoneCalled, $this->tMysqli);
             $Chamado->fill();
             $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findAllMyPhoneCalled() {
        $IDUsuario = Dracosys::getLoggedUser()->getIDUsuario();
        $StSql = TQuery::getSqlFindAllMyPhoneCalled($IDUsuario);
        $this->initConnection();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArPhoneCalled = mysqli_fetch_assoc($tQuery)) {
            $Chamado = Chamado::calledFactory($ArPhoneCalled, $this->tMysqli);
            $Chamado->fill();
            $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findAllExternalCalledBySector() {
        $this->initConnection();
        $StSql = TQuery::getSqlFindExternalCalledBySector(Dracosys::getLoggedUser()->getTDepartamento()->getSetores(true)->getIDSetor());
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArCalledData = mysqli_fetch_assoc($tQuery)) {
            $Chamado = Chamado::calledFactory($ArCalledData, $this->tMysqli);
            $Chamado->fill();
            $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findAllMyExternalCalled() {
        $IDUsuario = Dracosys::getLoggedUser()->getIDUsuario();
        $this->initConnection();
        $StSql = TQuery::getSqlFindAllMyExternalCalled($IDUsuario);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArCalledData = mysqli_fetch_assoc($tQuery)) {
            $Chamado = Chamado::calledFactory($ArCalledData, $this->tMysqli);
            $Chamado->fill();
            $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findAllPriorityExternalCalled() {
        $IDSetor = Dracosys::getLoggedUser()->getTDepartamento()->getSetores(true)->getIDSetor();
        $this->initConnection();
        $StSql = TQuery::getSqlFindPriorityExternalCalled($IDSetor);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArCalledData = mysqli_fetch_assoc($tQuery)) {
            $Chamado = Chamado::calledFactory($ArCalledData, $this->tMysqli);
            $Chamado->fill();
            $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findCalledByCustomer($IDCliente) {
        $this->initConnection();
        $this->tMysqli->escape($IDCliente);
        $ArCalleds = array();
        $StSql = TQuery::getSqlFindCalledByCustomer($IDCliente);
        $tQuery = $this->tMysqli->exec($StSql);
        while ($ArCalledData = mysqli_fetch_assoc($tQuery)) {
             $Chamado = Chamado::calledFactory($ArCalledData, $this->tMysqli);
             $Chamado->fill();
             $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findAllInternalCalled() {
        $this->initConnection();
        $StSql = TQuery::getSqlFindAllInternalCalled();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArCalledData = mysqli_fetch_assoc($tQuery)) {
            $Chamado = Chamado::calledFactory($ArCalledData, $this->tMysqli);
            $Chamado->fill();
            $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findInternalCalledBySector() {
        $this->initConnection();
        $StSql = TQuery::getSqlFindInternalCalledBySector(Dracosys::getLoggedUser()->getTDepartamento()->getSetores(true)->getIDSetor());
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArCalledData = mysqli_fetch_assoc($tQuery)) {
            $Chamado = Chamado::calledFactory($ArCalledData, $this->tMysqli);
            $Chamado->fill();
            $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findAllMyInternalCalledWaintingCustomer() {
        $IDUsuario = Dracosys::getLoggedUser()->getIDUsuario();
        $this->initConnection();
        $StSql = TQuery::getSqlFindInternalCalledWaitingCustomerByUser($IDUsuario);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArCalledData = mysqli_fetch_assoc($tQuery)) {
            $Chamado = Chamado::calledFactory($ArCalledData, $this->tMysqli);
            $Chamado->fill();
            $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findAllMyExternalCalledWatingCustomer() {
        $IDUsuario = Dracosys::getLoggedUser()->getIDUsuario();
        $this->initConnection();
        $StSql = TQuery::getSqlFindExternalCalledWaitingCustomerByUser($IDUsuario);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArCalledData = mysqli_fetch_assoc($tQuery)) {
            $Chamado = Chamado::calledFactory($ArCalledData, $this->tMysqli);
            $Chamado->fill();
            $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function countNumberOfAnwernsExternalCalledFromLoggedUser($ArPeriod) {
        $IDUsuario = Dracosys::getLoggedUser()->getIDUsuario();
        $this->initConnection();
        $StSql = TQuery::getSqlTotalExternalCalledByUser($IDUsuario, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $numberExternalCalled = $this->tMysqli->result(0, 'NumberExternalCalled');
        $this->closeConnection();
        return $numberExternalCalled;
    }

    public function countNumberOfAnwernsInternalCalledFromLoggedUser($ArPeriod) {
        $IDUsuario = Dracosys::getLoggedUser()->getIDUsuario();
        $this->initConnection();
        $StSql = TQuery::getSqlTotalInternalCalledByUser($IDUsuario, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $numberInternalCalled = $this->tMysqli->result(0, 'NumberInternalCalled');
        $this->closeConnection();
        return $numberInternalCalled;
    }

    public function countNumberOfAnwernsPhoneCalledFromLoggedUser($ArPeriod) {
        $IDUsuario = Dracosys::getLoggedUser()->getIDUsuario();
        $this->initConnection();

        $StSql = TQuery::getSqlTotalPhoneCalledByUser($IDUsuario, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $numberPhoneCalled = $this->tMysqli->result(0, 'NumberPhoneCalled');
        $this->closeConnection();
        return $numberPhoneCalled;
    }

    public function saveInternalCalled($ArDados) {
        $this->initTransaction();
        $this->tMysqli->escape($ArDados);

        $Chamado = new ChamadoInterno($ArDados, $this->tMysqli);
        $Chamado->loadDataCreatedCall();
        $StSqlNewCalled = TQuery::getSqlInsertNewCalled($Chamado);
        $this->tMysqli->exec($StSqlNewCalled);
        $IDChamado = $this->tMysqli->lastInsertId();

        $Chamado->setDefaultDataInAllMessages($IDChamado);
        $StSqlNewMessage = TQuery::getSqlInsertNewMessage($Chamado->getFirstMessage());
        $this->tMysqli->exec($StSqlNewMessage);

        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function countNumberOfInternalAnswersByUser($IDUsuario, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDUsuario);
        $StSql = TQuery::getSqlTotalAnswersInternalCalledByUser($IDUsuario, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberInternalCalled');
    }

    public function countNumberOfExternalAnswersByUser($IDUsuario, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDUsuario);

        $StSql = TQuery::getSqlTotalExternalAnswersByUser($IDUsuario, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberExternalCalled');
    }

    public function countNumberOfPhoneCalledByUser($IDUsuario, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDUsuario);

        $StSql = TQuery::getSqlTotalPhoneCalledByUser($IDUsuario, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberPhoneCalled');
    }

    public function countNumberOfNotClosedCalledByUser($IDUsuario, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDUsuario);

        $StSql = TQuery::getSqlTotalNotClosedCalledByUser($IDUsuario, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberNotClosedCalled');
    }

    public function countNumberOf1AnswerClosedCalledByUser($IDUsuario, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDUsuario);

        $StSql = TQuery::getSqlTotal1AnswerClosedCalledByUser($IDUsuario, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberOf1AnswerClosedCalled');
    }

    public function findAllCancelledCalled() {
        $this->initConnection();
        $StSql = TQuery::getSqlFindCancelledCalledByStatus('ATENDENTE');
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArDadosChamado = mysqli_fetch_assoc($tQuery)) {
            $Chamado = new ChamadoCancelamento($ArDadosChamado, $this->tMysqli);
            $Chamado->fill();
            $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function countNumberOfInternalAnswersBySector($IDSetor, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDSetor);

        $StSql = TQuery::getSqlTotalInternalCalledBySector($IDSetor, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberInternalCalled');
    }

    public function countNumberOfExternalAnswersBySector($IDSetor, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDSetor);

        $StSql = TQuery::getSqlTotalExternalCalledBySector($IDSetor, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberExternalCalled');
    }

    public function countNumberOfPhoneCalledBySector($IDSetor, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDSetor);

        $StSql = TQuery::getSqlTotalPhoneCalledBySector($IDSetor, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberPhoneCalled');
    }

    public function countNumberOfNotClosedCalledBySector($IDSetor, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDSetor);

        $StSql = TQuery::getSqlTotalNotClosedCalledBySector($IDSetor, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberNotClosedCalled');
    }

    public function countNumberOf1AnswerClosedCalledBySector($IDSetor, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDSetor);

        $StSql = TQuery::getSqlTotal1AnswerClosedCalledBySector($IDSetor, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberOf1AnswerClosedCalled');
    }

    public function countNumberOfInternalAnswersByDepartament($IDDepartamento, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDDepartamento);

        $StSql = TQuery::getSqlTotalInternalCalledByDepartament($IDDepartamento, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberInternalCalled');
    }

    public function countNumberOfExternalAnswersByDepartament($IDDepartamento, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDDepartamento);

        $StSql = TQuery::getSqlTotalExternalCalledByDepartament($IDDepartamento, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberExternalCalled');
    }

    public function countNumberOfPhoneCalledByDepartament($IDDepartamento, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDDepartamento);

        $StSql = TQuery::getSqlTotalPhoneCalledByDepartament($IDDepartamento, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberPhoneCalled');
    }

    public function countNumberOfNotClosedCalledByDepartament($IDDepartamento, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDDepartamento);

        $StSql = TQuery::getSqlTotalNotClosedCalledByDepartament($IDDepartamento, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberNotClosedCalled');
    }

    public function countNumberOf1AnswerClosedCalledByDepartament($IDDepartamento, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDDepartamento);

        $StSql = TQuery::getSqlTotal1AnswerClosedCalledByDepartament($IDDepartamento, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberOf1AnswerClosedCalled');
    }

    public function closeCalled($ArFilter, $Mensagem){
        $Called = $this->findById($ArFilter['IDChamado']);
        $Customer = $this->clienteService->findSimpleCustomerById( $Called->getIDCliente() );
        $StSqlUpdateCalled = TQuery::getSqlUpdateCalledByFilter($ArFilter);
        $StSqlReleaseHistory = TQuery::getSqlReleaseMessageInHistory($Mensagem);
        $StSqlCancelCustomer = TQuery::getSqlCancelCustomer( $Called->getIDCliente() );
        $StSqlInsertCancelledHistory = TQuery::getSqlInsertHistorySituation($Customer->getFinanceiro()->getIDFinanceiro(), 'CANCELADO');
        $this->initTransaction();
        $this->tMysqli->exec($StSqlUpdateCalled);
        $this->tMysqli->exec($StSqlReleaseHistory);
        $this->tMysqli->exec($StSqlCancelCustomer);
        $this->tMysqli->exec($StSqlInsertCancelledHistory);
        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function findAllCancelledCalledWaitingCustomer() {
        $this->initConnection();
        $StSql = TQuery::getSqlFindCancelledCalledByStatus('CLIENTE');
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCalleds = array();
        while ($ArDadosChamado = mysqli_fetch_assoc($tQuery)) {
             $Chamado = Chamado::calledFactory($ArDadosChamado, $this->tMysqli);;
             $Chamado->fill();
             $ArCalleds[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCalleds;
    }

    public function findAllClosedCancelledCalled() {
        $this->initConnection();
        $StSql = TQuery::getSqlFindCancelledCalledByStatus('FECHADO');
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCancelledCalled = array();
        while ($ArDadosChamado = mysqli_fetch_assoc($tQuery)) {
            $Chamado = Chamado::calledFactory($ArDadosChamado, $this->tMysqli);
            $Chamado->fill();
            $ArCancelledCalled[] = $Chamado;
        }
        $this->closeConnection();
        return $ArCancelledCalled;
    }

    public function addToFavourite($IDChamado, $IDUsuario) {
        $this->initConnection();
        $StSql = TQuery::getSqlAddToFavourite($IDChamado, $IDUsuario);
        $this->tMysqli->exec($StSql);
        $BoReturn = $this->tMysqli->lastQueryReturn();
        $this->closeConnection();
        return $BoReturn;
    }

    public function removeFromFavourite($IDChamado, $IDUsuario) {
        $this->initConnection();
        $StSql = TQuery::getSqlRemoveFromFavourite($IDChamado, $IDUsuario);
        $this->tMysqli->exec($StSql);
        $BoReturn = $this->tMysqli->lastQueryReturn();
        $this->closeConnection();
        return $BoReturn;
    }

    public function findCalledByMessage($IDMensagem) {
        $this->initConnection();
        $this->tMysqli->escape($IDMensagem);
        $StSql = TQuery::getSqlFindCalledByMessage($IDMensagem);
        $tQuery = $this->tMysqli->exec($StSql);
        while ($ArDadosChamado = mysqli_fetch_assoc($tQuery)) {
            $Chamado = Chamado::calledFactory($ArDadosChamado, $this->tMysqli);
            $Chamado->fill();
        }
        $this->closeConnection();
        return isset($Chamado) ? $Chamado : false;
    }

    public function readingCalled($IDChamado) {
        $this->initConnection();
        $this->tMysqli->exec('UPDATE dra__Chamados SET BoLendo = "TRUE" WHERE IDChamado = "'.$IDChamado.'"');
        $BoReturn = $this->tMysqli->lastQueryReturn();
        $this->closeConnection();
        return $BoReturn;
    }

    public function notReadingCalled($IDChamado) {
        $this->initConnection();
        $this->tMysqli->exec('UPDATE dra__Chamados SET BoLendo = "FALSE" WHERE IDChamado = "'.$IDChamado.'"');
        $BoReturn = $this->tMysqli->lastQueryReturn();
        $this->closeConnection();
        return $BoReturn;
    }

    public function isReading($IDChamado) {
        $this->initConnection();
        $this->tMysqli->exec('SELECT BoLendo FROM dra__Chamados WHERE IDChamado = "'.$IDChamado.'"');
        $BoLendo = $this->tMysqli->result(0, 'BoLendo');
        $this->closeConnection();
        return $BoLendo == 'TRUE';

    }

}
?>

